package com.myjob.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.myjob.config.Constants;
import com.myjob.entity.Advertisement;
import com.myjob.enums.JobEnums;
import com.myjob.enums.RedisEnums;
import com.myjob.exception.OptionException;
import com.myjob.response.Result;
import com.myjob.service.AdvertisementService;
import com.myjob.utils.RedisUtil;
import com.myjob.utils.UtilValidate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Chen Yuan
 * @create 2021-03-10  10:59
 */
@RestController
@Slf4j
@RequestMapping("/account")
public class AdvertisementController {
	@Autowired
	private AdvertisementService advertisementService;
	@Autowired
	private RedisUtil redisUtil;

	@GetMapping("/advertise")
	public Result selectAdvertise(@RequestParam("currentPage") Integer currentPage,
	                              @RequestParam(value = "pageSize", required = false) Integer pageSize,
	                              @RequestParam(value = "level", required = false) String level,
	                              @RequestParam(value = "searchValue", required = false) String searchValue){
		Result result = null;
		try {
			if (currentPage == -1){
				QueryWrapper<Advertisement> wrapper = new QueryWrapper<>();
				wrapper.eq("level", level);
				List<Advertisement> list = advertisementService.list(wrapper);
				result = Result.succeed(list,"查询所有 "+level+" 广告");
			}else {
				result = advertisementService.selectAdvertise(currentPage, pageSize, level, searchValue);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new OptionException("条件查询广告信息失败");
		}
		return result;
	}

	@PutMapping("/advertise")
	public Result addAdvertise(@RequestBody Advertisement advertisement){
		if (UtilValidate.isEmpty(advertisement)) {
			return Result.failed("参数为空");
		}

		Result result = null;
		try {

			String photoPath = (String) redisUtil.get(RedisEnums.KEY_IN_REDIS.ADV_PHOTO.getValue()
					+ advertisement.getPhotoId() + ".png");
			log.info(photoPath);
			if (UtilValidate.isNotEmpty(photoPath)) {
				advertisement.setPhoto(photoPath);
			}

			boolean save = advertisementService.save(advertisement);
			if (save){
				result = Result.succeed("添加成功");
			}else {
				result = Result.failed("添加失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new OptionException("添加广告信息失败");
		}
		return result;
	}

	@PostMapping("/advertise")
	public Result updateAdvertise(@RequestBody Advertisement advertisement){
		if (UtilValidate.isEmpty(advertisement)) {
			return Result.failed("参数为空");
		}

		Result result = null;
		try {
			if (UtilValidate.isNotEmpty(advertisement.getPhotoId())) {
				String photoPath = (String) redisUtil.get(RedisEnums.KEY_IN_REDIS.ADV_PHOTO.getValue()
						+ advertisement.getPhotoId() + ".png");
				if (UtilValidate.isNotEmpty(photoPath)) {
					advertisement.setPhoto(photoPath);
				}
			}

			boolean update = advertisementService.updateById(advertisement);
			if (update){
				result = Result.succeed("修改成功");
			}else {
				result = Result.failed("修改失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new OptionException("修改广告信息失败");
		}
		return result;
	}

	@DeleteMapping("/advertise")
	public Result deleteAdvertise(@RequestParam("id") String id){
		Result result = null;
		try {
			if (UtilValidate.isEmpty(id)) {
				return Result.failed("参数为空");
			}

			boolean remove = advertisementService.removeById(id);
			if (remove){
				result = Result.succeed("删除成功");
			}else {
				result = Result.failed("删除失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new OptionException("删除广告信息失败");
		}
		return result;
	}
}
